/**
 *====================================================
 * 文件名称: FinanceLetterBlockingQueue.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年10月24日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.context;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuexunit.bulletin.letter.action.v1.LetterActionApi;
import com.yuexunit.core.ConfigPropertites;
import com.yuexunit.finance.context.FinanceLetter.LetterTargetEnum;
import com.yuexunit.finance.context.FinanceLetter.LetterTypeEnum;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.LogisticsClerkEntity;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.member.service.LogisticsClerkService;
import com.yuexunit.finance.project.entity.AccountTerminalEntity;
import com.yuexunit.finance.project.service.AccountTerminalService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.umeng.push.AppTypeEnum;
import com.yuexunit.umeng.push.PushMessageUtil;

/**
 * @ClassName: FinanceLetterBlockingQueue
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年10月24日 下午9:19:46
 */
public class FinanceLetterBlockingQueue {

	private static final Logger logger = LoggerFactory.getLogger(FinanceLetterBlockingQueue.class);

	private static Integer FILE_QUEUE_SIZE = 10;

	private static BlockingQueue<FinanceLetter> LETTER_QUEUE = new ArrayBlockingQueue<FinanceLetter>(FILE_QUEUE_SIZE);

	private static void sendLetter(FinanceLetter letter) {
		String template = ConfigPropertites.getProperties(letter.getTemplate());
		String content = String.format(template, letter.getParams());

		LetterActionApi letterAction = ComponentFactory.loadClass(LetterActionApi.class);
		Long accountId = letter.getAccountId();
		if (LetterTargetEnum.ENTERPRISE == letter.getLetterTargetEnum()) {
			if (letter.getEnterpriseId() != null) {
				EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
				EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(letter.getEnterpriseId());
				accountId = enterprise.getAccountId();
//				accountId = 141L;
			} else {
				logger.error("the EnterpriseId is null");
			}
		} else if (LetterTargetEnum.LOGISTICS == letter.getLetterTargetEnum()) {
			if (letter.getClerkId() != null) {
				LogisticsClerkService clerkService = ComponentFactory.loadClass(LogisticsClerkService.class);
				LogisticsClerkEntity clerk = clerkService.inqueryLogisticsClerk(letter.getClerkId());
				accountId = clerk.getAccountId();
			}
		} else if (LetterTargetEnum.PLATFORM == letter.getLetterTargetEnum()) {
			if (letter.getAccountId() != null) {
				accountId = letter.getAccountId();
			}
		}
//		try {
//			FileWriter output = new FileWriter("C://test2.txt");
//			BufferedWriter bf = new BufferedWriter(output);
//			bf.write("accountId=" + accountId + "\r\n");
//			bf.flush();
//			bf.close();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		if (accountId != null) {
			letterAction.createLetter(new Long[] { accountId }, content, letter.getTitle(), letter.getLetterType().ordinal());
			AccountTerminalService accountTerminalService = ComponentFactory.loadClass(AccountTerminalService.class);
			AccountTerminalEntity terminal = accountTerminalService.inquireAccountTerminal(accountId);
			if (LetterTargetEnum.ENTERPRISE == letter.getLetterTargetEnum()) {
				PushMessageUtil.pushMessage(AppTypeEnum.FINANCE, terminal.getTerminalSerial(), letter.getTitle(), content, 1);
			} else if (LetterTargetEnum.LOGISTICS == letter.getLetterTargetEnum()) {
				PushMessageUtil.pushMessage(AppTypeEnum.TALLY, terminal.getTerminalSerial(), letter.getTitle(), content, 2);
			} else if (LetterTargetEnum.PLATFORM == letter.getLetterTargetEnum()) {
				logger.info("============================" + terminal.getTerminalSerial() + content);
				PushMessageUtil.pushMessage(AppTypeEnum.PLATFORM, terminal.getTerminalSerial(), letter.getTitle(), content, 2);
			}
//			try {
//				FileWriter output = new FileWriter("C://test2.txt");
//				BufferedWriter bf = new BufferedWriter(output);
//				bf.write("accountId=" + accountId + "\r\n");
//				bf.write("terminalSerial=" + terminal.getTerminalSerial() + "\r\n");
//				bf.write("title=" + letter.getTitle() + "\r\n");
//				bf.write("content=" + content + "\r\n");
//				bf.flush();
//				bf.close();
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
	}

	private static void start() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (LETTER_QUEUE.size() != 0) {
					try {
						FinanceLetter letter = LETTER_QUEUE.take();
						sendLetter(letter);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	public static void putEnterpriseFinanceLetter(Long enterpriseId, String title, String template, Object... params) {
		try {
			FinanceLetter letter = new FinanceLetter();
			letter.setEnterpriseId(enterpriseId);
			letter.setLetterTargetEnum(LetterTargetEnum.ENTERPRISE);
			letter.setLetterType(LetterTypeEnum.FINANCE);
			letter.setParams(params);
			letter.setTemplate(template);
			letter.setTitle(title);
			LETTER_QUEUE.put(letter);
			start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void putPlatformFinanceLetter(Long accountId, String title, String template, Object... params) {
		try {
			FinanceLetter letter = new FinanceLetter();
			letter.setAccountId(accountId);
			letter.setLetterTargetEnum(LetterTargetEnum.PLATFORM);
			letter.setLetterType(LetterTypeEnum.FINANCE);
			letter.setParams(params);
			letter.setTemplate(template);
			letter.setTitle(title);
			LETTER_QUEUE.put(letter);
			start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void putEnterpriseExceptionLetter(Long enterpriseId, String title, String template, Object... params) {
		try {
			FinanceLetter letter = new FinanceLetter();
			letter.setEnterpriseId(enterpriseId);
			letter.setLetterTargetEnum(LetterTargetEnum.ENTERPRISE);
			letter.setLetterType(LetterTypeEnum.EXCEPTION);
			letter.setParams(params);
			letter.setTemplate(template);
			letter.setTitle(title);
			LETTER_QUEUE.put(letter);
			start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void putEnterpriseRemindedLetter(Long enterpriseId, String title, String template, Object... params) {
		try {
			FinanceLetter letter = new FinanceLetter();
			letter.setEnterpriseId(enterpriseId);
			letter.setLetterTargetEnum(LetterTargetEnum.ENTERPRISE);
			letter.setLetterType(LetterTypeEnum.REMINDED);
			letter.setParams(params);
			letter.setTemplate(template);
			letter.setTitle(title);
			LETTER_QUEUE.put(letter);
			start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void putCompanyTallyTaskLetter(Long clerkId, Long companyId, String title, String template, Object... params) {
		try {
			FinanceLetter letter = new FinanceLetter();
			letter.setClerkId(clerkId);
			letter.setCompanyId(companyId);
			letter.setLetterTargetEnum(LetterTargetEnum.LOGISTICS);
			letter.setLetterType(LetterTypeEnum.REMINDED);
			letter.setParams(params);
			letter.setTemplate(template);
			letter.setTitle(title);
			LETTER_QUEUE.put(letter);
			start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
